import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '../views/Login.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue')
      },
      {
        path: 'file-manager',
        name: 'FileManager',
        component: () => import('../views/FileManager.vue')
      },
      {
        path: 'photo',
        name: 'Photo',
        component: () => import('../views/photo/index.vue'),
        meta: {
          title: '照片优化'
        }
      },
      {
        path: 'log',
        name: 'Log',
        component: () => import('../views/log/index.vue'),
        meta: {
          title: '日志分析'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.meta.requiresAuth && !token) {
    next('/login')
  } else {
    next()
  }
})

export default router 